package com.gxa.meiye.admin.shop.controller;

import com.gxa.meiye.admin.shop.service.ShopInstallService;
import com.gxa.meiye.core.common.ResponseData;
import com.gxa.meiye.core.constant.MassageConstant;
import com.gxa.meiye.core.pojo.shopManagement.Shop;
import com.gxa.meiye.core.utils.QiniuUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @Author cc
 * @PackageName meiye
 * @Package com.gxa.meiye.admin.shop.controller
 * @Date 2022/7/25 10:45
 * @Version 1.0
 */
@RestController
@Api(tags ="店面设置")
@RequestMapping("shopInstall")
public class AdminShopInstallController {
    @Autowired
    private ShopInstallService shopInstallService;
    @PostMapping("editCollectionInformation")
    @ApiOperation("根据账号邮箱修改收款信息")
    public ResponseData updateShopCollectionInformation(Shop shop){
        ResponseData rd = new ResponseData();
        shopInstallService.editCollectionInformation(shop);
        rd.setCode(200);
        rd.setMsg("修改成功");
        return rd;
    }

    @PostMapping("editCompanyInformation")
    @ApiOperation("根据账号邮箱修改企业信息")
    public ResponseData updateShopCompanyInformation(MultipartFile imagFile1, MultipartFile imgFile2, Shop shop){
        ResponseData rd = new ResponseData();

        if((null != imagFile1)&&(null == imgFile2)){
          try {
              //1.获取原始第一个文件名
              String originalFilename1 = imagFile1.getOriginalFilename();
              //2.生成新的文件名称
              int index1 = originalFilename1.lastIndexOf(".");//djx.jpg
              String suffix1 = originalFilename1.substring(index1);//.jpg
              String filename1 = UUID.randomUUID().toString() + suffix1;
              //3.调用工具类，上传图片到七牛云
              QiniuUtils.upload2Qiniu(imagFile1.getBytes(), filename1);
              shop.setBusinessLicense(filename1);
          }catch (Exception e) {
              e.printStackTrace();
              rd.setMsg(MassageConstant.PIC_UPLOAD_FAIL);
          }
        }else if((null == imagFile1)&&(null != imgFile2)){
            try{
                //1.获取原始第二个文件名
                String originalFilename2 = imgFile2.getOriginalFilename();
                //2.生成新的文件名称
                int index2 = originalFilename2.lastIndexOf(".");//djx.jpg
                String suffix2 = originalFilename2.substring(index2);//.jpg
                String filename2 = UUID.randomUUID().toString() + suffix2;
                //3.调用工具类，上传图片到七牛云
                QiniuUtils.upload2Qiniu(imgFile2.getBytes(), filename2);
                //4.返回结果
                shop.setStorePhotos(filename2);
            }catch (Exception e) {
                e.printStackTrace();
                rd.setMsg(MassageConstant.PIC_UPLOAD_FAIL);
            }
        }else {
            try{
                //1.获取原始第一个文件名
                String originalFilename1 = imagFile1.getOriginalFilename();
                //2.生成新的文件名称
                int index1 = originalFilename1.lastIndexOf(".");//djx.jpg
                String suffix1 = originalFilename1.substring(index1);//.jpg
                String filename1 = UUID.randomUUID().toString() + suffix1;
                //3.调用工具类，上传图片到七牛云
                QiniuUtils.upload2Qiniu(imagFile1.getBytes(), filename1);
                shop.setBusinessLicense(filename1);
                //1.获取原始第二个文件名
                String originalFilename2 = imgFile2.getOriginalFilename();
                //2.生成新的文件名称
                int index2 = originalFilename2.lastIndexOf(".");//djx.jpg
                String suffix2 = originalFilename2.substring(index2);//.jpg
                String filename2 = UUID.randomUUID().toString() + suffix2;
                //3.调用工具类，上传图片到七牛云
                QiniuUtils.upload2Qiniu(imgFile2.getBytes(), filename2);
                //4.返回结果
                shop.setStorePhotos(filename2);

            }catch (Exception e) {
                e.printStackTrace();
                rd.setMsg(MassageConstant.PIC_UPLOAD_FAIL);
            }
        }
        shopInstallService.editShopCompanyInformation(shop);
        rd.setCode(200);
        rd.setMsg("修改成功");
        return rd;
    }


}
